//
//  Block.m
//  CarDemo
//
//  Created by huang kun on 8/10/10.
//  Copyright 2010 UESTC. All rights reserved.
//

#import "Block.h"

@implementation NextBlock

@synthesize type;

- (void) setType:(int)t {
	if ( t >= kblockType1 && t <= kblockType5 ) {
		type = t;
		NSString *file = [NSString stringWithFormat:@"bigBlock%d.png",type];
		[self setTexture:[[CCTextureCache sharedTextureCache] addImage:file]];
		[self setTextureRect:CGRectMake(0, 0, 48, 48)];
	}
	else {
		type = kblockTypeDefault;
		[self setTextureRect:CGRectZero];
	}
}

@end


@implementation Block

@synthesize connect;
@synthesize delegate;
@synthesize isTouchEnable;


- (void) dealloc {
	[self setIsTouchEnable:NO];
	[self removeAllChildrenWithCleanup:YES];
	[super dealloc];
}

- (BlockShape) getBlock {
	BlockShape shape;
	if(up)shape.up = kavaliable;
	else shape.up = kblocked;
	if(down)shape.down = kavaliable;
	else shape.down = kblocked;
	if(left)shape.left = kavaliable;
	else shape.left = kblocked;
	if(right)shape.right = kavaliable;
	else shape.right = kblocked;
	
	shape.isBlock = YES;
	return shape;
}


- (void) setConnect:(BOOL)c {
	if(connect == c)return;
	connect = c;
	NSString *file = nil;
	if(connect) 
		file = [NSString stringWithFormat:@"redBlock%d.png",type];
	else
		file = [NSString stringWithFormat:@"blueBlock%d.png",type];


	[self setTexture:[[CCTextureCache sharedTextureCache] addImage:file]];
	[self setTextureRect:CGRectMake(0, 0, 38, 38)];
}


- (void) setType:(int)t {
	if ( t >= kblockType1 && t <= kblockType5 ) {
		type = t;
		switch (type) {
			case kblockType1:
			{
				up = NO;
				down = NO;
				left = NO;
				right = NO;
			}
				break;
			case kblockType2:
			{
				up = YES;
				down = YES;
				left = YES;
				right = YES;
			}
				break;
			case kblockType3:
			{
				up = YES;
				down = YES;
				left = NO;
				right = NO;
			}
				break;
			case kblockType4:
			{
				up = YES;
				down = NO;
				left = NO;
				right = YES;
			}
				break;
			case kblockType5:
			{
				up = YES;
				down = NO;
				left = YES;
				right = YES;
			}
				break;
			default:
				break;
		}
		[self setTexture:[[CCTextureCache sharedTextureCache] addImage:
						  [NSString stringWithFormat:@"blueBlock%d.png",type]]];
		[self setTextureRect:CGRectMake(0, 0, 38, 38)];
	}
}


+ (id) blockWithType:(int)t {
	return [[[self alloc] initWithType:t] autorelease];
}

- (id) initWithType:(int)t {
	if ( t >= kblockType1 && t <= kblockType5 && (self = [super init])) {
		[self setType:t];
		connect = NO;
		isTouchEnable = NO;
		return self;
	}
	else {
		return nil;
	}
}

- (void) onEnter {
	[super onEnter];
	[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
}
- (void) onExit {
	[[CCTouchDispatcher sharedDispatcher] removeDelegate:self];	
	[super onExit];
}

- (BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
	CGPoint p = [touch locationInView:[touch view]];
	p = [[CCDirector sharedDirector] convertToGL:p];
	
	
	if(isTouchEnable && CGRectContainsPoint(self.boundingBox, p)){
		[self rotate];
		return YES;
	}
	else {
		return NO;
	}
}

- (void) rotate {
	//顺时针旋转
	if ([self numberOfRunningActions] == 0)
		[self runAction:[CCSequence actions:[CCRotateBy actionWithDuration:0.5f angle:90],
					 [CCCallFunc actionWithTarget:self selector:@selector(update:)],nil]];
	
}

- (void) update:(id)sender {
	BOOL u = up;
	BOOL d = down;
	BOOL l = left;
	BOOL r = right;
	
	up = l;
	down = r;
	left = d;
	right = u;
	
	if(delegate)
		[delegate updateBlock:self];
}


@end
